package tiandang.core.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tiandang.core.dao.ManagerApplyMapper;
import tiandang.core.dao.ManagerMapper;
import tiandang.core.dao.RewardLogMapper;
import tiandang.core.error.BusinessException;
import tiandang.core.error.EmBusinessError;
import tiandang.core.model.Manager;
import tiandang.core.model.RewardLog;
import tiandang.core.service.ManagerService;
import tiandang.core.validator.ValidationResult;
import tiandang.core.validator.ValidatorImpl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ManagerServiceImpl implements ManagerService {

  @Autowired
  private ValidatorImpl validator;

  @Autowired
  private ManagerMapper managerMapper;

  @Autowired
  private RewardLogMapper rewardLogMapper;

  @Autowired
  private ManagerApplyMapper managerApplyMapper;

  @Override
  public Object getList(String name, Integer currentPage, Integer limit) {
    Map<String, Object> map = new HashMap<>();
    int offset = (currentPage - 1) * limit;
    List<Manager> managerList = managerMapper.getListByParams(name, limit, offset);
    int totalCount = managerMapper.countByParams(name);
    map.put("managerList", managerList);
    map.put("totalCount", totalCount);

    return map;
  }

  @Override
  public Manager getManagerById(Integer id) {
    return managerMapper.selectByPrimaryKey(id);
  }

  @Override
  @Transactional
  public void save(Manager manger) throws BusinessException {
    //校验入参
    ValidationResult result = validator.validate(manger);
    if (result.isHasErrors()) {
      throw new BusinessException(EmBusinessError.PARAMMETER_VALIDATION_ERROR, result.getErrMsg());
    }

    managerMapper.insertSelective(manger);

    //修改managerApply的状态
    if (manger.getManagerApplyId() != null) {
      managerApplyMapper.updateStatusById(manger.getManagerApplyId());
    }
  }


  @Override
  public void update(Manager manger) throws BusinessException {
    //校验入参
    ValidationResult result = validator.validate(manger);
    if (result.isHasErrors()) {
      throw new BusinessException(EmBusinessError.PARAMMETER_VALIDATION_ERROR, result.getErrMsg());
    }
    managerMapper.updateByPrimaryKeySelective(manger);
  }

  @Override
  public void delete(Integer id) {
    managerMapper.deleteByPrimaryKey(id);
  }

  @Override
  public void setFrozen(Integer id, Integer status) {
    managerMapper.setFrozen(id, status);
  }

  @Override
  public void addFrozenReward(Integer id, BigDecimal reward) {
    managerMapper.addFrozenReward(id, reward);
  }

  @Override
  public void resetFrozenRewardToNoBalance() {
    managerMapper.resetFrozenRewardToNoBalance();
  }

  @Override
  @Transactional
  public void balanceReward(Integer id) {
    Manager manager = managerMapper.selectByPrimaryKey(id);
    //1.将未结算佣金重置为 0,累加到已获佣金
    managerMapper.balanceReward(id);

    //2.加入到佣金日志表
    RewardLog rewardLog = new RewardLog();
    rewardLog.setManagerId(id);
    rewardLog.setReward(manager.getNobalanceMoney());

    rewardLogMapper.insertSelective(rewardLog);
  }

  @Override
  public List<Manager> getListByIdList(List<Integer> idList) {
    return managerMapper.getListByIdList(idList);
  }

  @Override
  public List<Manager> getAllNormal() {
    return managerMapper.getAllNormal();
  }

  @Override
  public void updateOpenidById(Integer id, String openid) {
    managerMapper.updateOpenidById(id, openid);
  }
}
